const Router = require('koa-router');
const router = new Router();
const {db} = require("../../models/db");

router.post('/goods/del', async ctx => {
    let id = ctx.request.body.id
    let tenantId = ctx.user.tenantId
    let c = await db.models['invoiceInfo'].count({
        where: {
            tenantId,
            invId: id
        }
    })
    if(c > 0) {
        return  ctx.err('商品已使用,不能删除')
    }
    c = await db.models['initGoods'].count({
        where: {
            tenantId,
            invId: id
        }
    })
    if(c > 0) {
        return  ctx.err('商品已使用,不能删除')
    }
    await db.transaction(async () => {
        await db.models['goods'].destroy({
            where: {
                tenantId,
                id
            }
        })
        await db.models['inventoryDetail'].destroy({
            where: {
                tenantId,
                invId: id
            }
        })
        await db.models['inventory'].destroy({
            where: {
                tenantId,
                invId: id
            }
        })


    })
    return ctx.suc('已删除')
})



module.exports = router;
